﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StuMana.UI
{
    public partial class SchoolLate : Form
    {
        #region Variables
        // datagridview parameters
        List<string>[] list;
        string[] columnName;
        string query;
        string[] columnNameReal;
        int page;
        int maxpage;
        static int rowperpage;

        private Services.DataviewService dataview = new Services.DataviewService();


        // Timer kiem tra thoi gian
        private System.Windows.Forms.Timer readTimer;


        // lay thoi gian
        DateTime dt = new DateTime();

        #endregion
        public SchoolLate()
        {
            InitializeComponent();
            schoolLate_initial();
        }
        
        // getPanel for mainform
        public Panel getPanel(int x, int y)
        {
            p_SchoolLate.Location = new System.Drawing.Point(x, y);
            return p_SchoolLate;
        }

        // initials
        public void schoolLate_initial()
        {

            // timer 
            //timer setting
            readTimer = new System.Windows.Forms.Timer();
            readTimer.Interval = 1000;
            readTimer.Tick += new EventHandler(readTimer_Tick);
            readTimer.Enabled = true;

            // mysql command
            columnName = new string[] { "TagID", "Name", "Class", "LateTime","InOut_status" };
            columnNameReal = new string[] { "TagID", "Name", "Class", "LateTime", "InOut_status" };
            query = "SELECT TagID, Name, Class, LateTime, InOut_status FROM studentinfo WHERE InOut_status=0";
            rowperpage = 10;
            page = 0;

            // buttons
            bt_next.Enabled = true;
            bt_previous.Enabled = true;

            dataview.setData(rowperpage, columnName, columnNameReal);
            dataview.DataOnDataGridViewInitial(dataGridView1, query, -1, -1, pages);
            initial_button();
        }

        /*
         * Timer handling
         */
        private void readTimer_Tick(object sender, EventArgs e)
        {
            dt = DateTime.Now;
            digitalWatch.Text = dt.ToLongTimeString();

            dataview.DataOnDataGridViewInitial(dataGridView1, query, -1, -1, pages);


            /*
             * Demo for listing school late students
             */
            if (dt.Second == int.Parse(demo_minute.Text))
            {
                int n = 0;
                string showInfor = "School late list at 07:00:00:\n";
                list = dataview.GetList();
                while (n != list[0].Count)
                {
                    if (list[3][n].ToString() == "07:00:00")                    
                        showInfor += list[1][n].ToString() + " " + list[3][n].ToString() + " \n";
                    n++;
                }
                MessageBox.Show(showInfor,"Warning");
            }
            /*
            if (dt.Hour == 7 && dt.Second == 0)
            {
                int n = 0;
                string showInfor = "School late list:\n";
                list = dataview.GetList();
                switch (dt.Minute)
                {
                    case 00:
                        while (n != list[0].Count)
                        {
                            if (list[3][n].ToString() == "07:00:00")
                                showInfor += list[1][n].ToString() + " " + list[3][n].ToString() + " \n";
                            n++;
                        }
                        break;
                    case 15:
                        while (n != list[0].Count)
                        {
                            if (list[3][n].ToString() == "07:15:00")
                                showInfor += list[1][n].ToString() + " " + list[3][n].ToString() + " \n";
                            n++;
                        }
                        break;
                    case 30:
                        while (n != list[0].Count)
                        {
                            if (list[3][n].ToString() == "07:30:00")
                                showInfor += list[1][n].ToString() + " " + list[3][n].ToString() + " \n";
                            n++;
                        }
                        break;
                    case 45:
                        while (n != list[0].Count)
                        {
                            if (list[3][n].ToString() == "07:45:00")
                                showInfor += list[1][n].ToString() + " " + list[3][n].ToString() + " \n";
                            n++;
                        }
                        break;
                    default:
                        break;
                }
                MessageBox.Show(showInfor);
            }           
            
            */
        }

        // buttons initials
        private void initial_button()
        {
            page = dataview.GetPage();
            maxpage = dataview.GetMaxPage();
            if (page == maxpage || maxpage == -1)
                bt_next.Enabled = false;
            bt_previous.Enabled = false;
        }
    }
}
